<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils\ResponseFactory;
use App\Http\Requests\admin\permission\AddPermissionRequest;
use App\Http\Requests\admin\permission\UpdatePermissionRequest;
use App\Models\Manager\Dictionary;
use App\Models\Manager\Permission;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;

/**
 *
 */
class PermissionController extends Controller
{
    //

    /**为用户添加权限
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    function addPermissionForUser(Request $request): \Illuminate\Http\JsonResponse
    {
        $id = auth()->user()->id;
        $user = User::all()->where('id', $id);
        if ($user != null) {
            $result = DB::table('users_permissions')->insert($request->all());
            if ($result != null) {
                return ResponseFactory::simpleResponse(true, "添加成功");
            } else {
                return ResponseFactory::simpleResponse(false, "添加失败");
            }
        } else {
            return ResponseFactory::simpleResponse(false, "用户不存在");
        }
    }

    /**添加权限
     * @param AddPermissionRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    function addPermission(AddPermissionRequest $request): \Illuminate\Http\JsonResponse
    {
        if (Dictionary::all()->where('id', $request->input('categoryId'))->count() == 0) {
            return ResponseFactory::simpleResponse(false, "数据字典中暂未添加该权限分类");
        }
        if (Permission::all()->where('permissionName', $request->input('permissionName'))->count() == 0) {
            $result = Permission::create($request->all());
            if ($result === null) {
                return ResponseFactory::simpleResponse(false, "添加失败");
            } else {
                return ResponseFactory::simpleResponse(true, "添加成功");
            }
        } else {

            return ResponseFactory::simpleResponse(false, "此权限已被添加");
        }
    }

    /**删除权限
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    function removePermission(int $id): \Illuminate\Http\JsonResponse
    {
        if (Permission::all()->where('id', $id)->count() > 0) {
            $result = Permission::destroy($id);
            if ($result > 0) {
                return ResponseFactory::simpleResponse(true, "删除成功");
            } else {
                return ResponseFactory::simpleResponse(false, "删除失败");
            }
        } else {
            return ResponseFactory::simpleResponse(false, "id不存在");
        }

    }


    /** 修改权限
     * @param UpdatePermissionRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    function updatePermission(UpdatePermissionRequest $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->all();
        if (Permission::all()->where('id', $params["id"])->count() > 0) {
            $result = Permission::where('id', $params['id'])->update($params);
            if ($result == null) {
                return ResponseFactory::simpleResponse(false, "修改失败");
            } else {
                return ResponseFactory::simpleResponse(true, "修改成功");
            }
        } else {
            return ResponseFactory::simpleResponse(false, "id值不存在");
        }
    }


    /**
     * 查询用户的所有权限
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    function selectPermission(): \Illuminate\Http\JsonResponse
    {
        if (User::all()->where('id', auth()->user()->id)->count() > 0) {
            $datas = User::all()->find(auth()->user()->id)->Permission()->get();
            return ResponseFactory::response($datas, null);
        } else {
            return ResponseFactory::simpleResponse(false, "用户不存在");
        }
    }

    function getRouteNames(Request $request)
    {
        $routes = app('router')->getRoutes();
        $rows = [];
        //$route->uri 获取路由
        //$route->methods 请求方式
        //$route->controller 控制器
        //$route->getName() 获取路由别名
        foreach ($routes as $route){
            $message['uri'] = $route->uri;
            if (strpos($route->uri, "api") === 0){
                $message['methods'] = $route->methods;
                $message['controller'] = $route->controller;
                $message['name'] = $route->getName();
                $rows[] = $message;
            }
        }
        return ResponseFactory::response(null,$rows);
    }
}
